<template>
  <div>
    <!-- <el-tabs
      v-if="courseArr.length"
      v-model="activeName"
      @tab-click="handleClick"
    >
      <el-tab-pane
        v-for="v in courseArr"
        :key="v.value"
        :label="v.msg"
        :name="v.value"
      ></el-tab-pane>
    </el-tabs> -->
    <el-table :data="tableData" style="width: 100%" stripe max-height="600">
      <el-table-column fixed prop="sort" label="排序" width="60">
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column prop="createName" label="创建人" width="120">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="140">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button
              type="primary"
              size="mini"
              @click="handleEdit(row)"
              v-if="activePro.length > 0"
              :disabled="$button(activePro[1]) == false ? true : false"
              class="jurisdiction"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              v-if="activePro.length > 0"
              :disabled="$button(activePro[2]) == false ? true : false"
              @click="handleDelete(row)"
              class="jurisdiction"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      style="margin-top: 20px"
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import { tbLabelQuery, tbLabelDelete } from '@/api/admin/system/label'
export default {
  data() {
    return {
      activeName: this.courseArr[0].value || 1,
      // activeName: 6,
      listQuery: {
        page: 1,
        limit: 10,
        type: '1',
        labelType: 1,
      },
      tableData: [],
      pageObj: {},
      activePro: [],
    }
  },
  props: ['courseArr', 'proList'],
  watch: {
    $route: {
      immediate: true,
      handler() {
        this.activeName = this.courseArr[0].value
        this.listQuery.type = this.activeName
        this.listQuery.labelType = this.$route.meta.labelType
        this.activePro = this.proList[this.listQuery.labelType - 1]
        this.getList()
      },
    },
  },
  mounted() {
    this.$bus.$on('refreshLabelList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    this.$bus.$off('refreshLabelList')
  },
  methods: {
    getList() {
      this.listQuery.type = this.activeName
      tbLabelQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.pageObj = res.data
      })
    },
    handleClick() {
      this.$bus.$emit('changeType', this.activeName)
      this.getList()
    },
    handleEdit(row) {
      this.$bus.$emit('showDialogLabelAdd', row)
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      tbLabelDelete(row.id).then((res) => {
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>